package com.alg.local.gateway.config;

import jodd.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.config.SentinelServersConfig;
import org.redisson.config.SingleServerConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

/**
 * @author al
 * @date 2022/3/3 17:39
 * @description
 */
@Slf4j
@Configuration
public class RedissonConfig {

    @Autowired
    SingleRedisProps singleRedisProps;

    @Autowired
    SentinelRedisProps sentinelRedisProps;

    @Autowired
    ClusterRedisProps clusterRedisProps;

    @Bean
    public RedissonClient getRedissonClient() {
        String clusterModel = singleRedisProps.getModel();
        Config config = new Config();
        log.info("initial [{}}] clusterModel Redission-Client", clusterModel);
        if ("single".equalsIgnoreCase(clusterModel)) {
            SingleServerConfig singleServerConfig = config.useSingleServer().setDatabase(singleRedisProps.getDatabase())
                    .setAddress("redis://" + singleRedisProps.getHost() + ":" + singleRedisProps.getPort());
            if (!StringUtil.isEmpty(singleRedisProps.getPassword())) {
                singleServerConfig.setPassword(singleRedisProps.getPassword());
            }
        } else if ("sentinel".equalsIgnoreCase(clusterModel)) {
            SentinelServersConfig sentinelServersConfig = config.useSentinelServers()
                    .setDatabase(singleRedisProps.getDatabase()).setMasterName(sentinelRedisProps.getMaster());
            if (!StringUtil.isEmpty(singleRedisProps.getPassword())) {
                sentinelServersConfig.setPassword(singleRedisProps.getPassword());
            }
            List<String> nodes = sentinelRedisProps.getNodes();
            for (String node : nodes) {
                sentinelServersConfig.addSentinelAddress("redis://" + node);
            }
        } else if ("cluster".equalsIgnoreCase(clusterModel)) {
            if (StringUtil.isEmpty(singleRedisProps.getPassword())) {
                config.useClusterServers().setNodeAddresses(clusterRedisProps.getNodes());
            } else {
                config.useClusterServers().setPassword(singleRedisProps.getPassword()).setNodeAddresses(clusterRedisProps.getNodes());
            }
        } else {
            log.info("can not find clusterModel config, initial default single Redission-Client");
            SingleServerConfig singleServerConfig = config.useSingleServer().setDatabase(singleRedisProps.getDatabase())
                    .setAddress("redis://" + singleRedisProps.getHost() + ":" + singleRedisProps.getPort());
            if (!StringUtil.isEmpty(singleRedisProps.getPassword())) {
                singleServerConfig.setPassword(singleRedisProps.getPassword());
            }
        }

        return Redisson.create(config);
    }

}
